library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)
library(haven)
library(RColorBrewer)

# Chapter 8

### Figure 8.1

# Wave 2
# Create Bar plot with percentage Movies per age group
summary(factor(df$Q32))
summary(factor(df$AGE7))

df$MOVIES <- NA
df$MOVIES[df$Q32 < 77] <- 0
df$MOVIES[df$Q32 == 2] <- 1
summary(factor(df$MOVIES))

df2 <- df[!is.na(df$MOVIES),]
summary(factor(df2$MOVIES))

# Create Survey design
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )


# Calculate the means by age
g1 <- "18-24"
g2 <- "25-34"
g3 <- "35-44"
g4 <- "45-54"
g5 <- "55-64"
g6 <- "65-74"
g7 <- "75+"
a1 <- mean(w2_design$variables$MOVIES[w2_design$variables$AGE7==1])
a2 <- mean(w2_design$variables$MOVIES[w2_design$variables$AGE7==2])
a3 <- mean(w2_design$variables$MOVIES[w2_design$variables$AGE7==3])
a4 <- mean(w2_design$variables$MOVIES[w2_design$variables$AGE7==4])
a5 <- mean(w2_design$variables$MOVIES[w2_design$variables$AGE7==5])
a6 <- mean(w2_design$variables$MOVIES[w2_design$variables$AGE7==6])
a7 <- mean(w2_design$variables$MOVIES[w2_design$variables$AGE7==7])

ag <- c(g1,g2,g3,g4,g5,g6,g7)
mn <- c(a1,a2,a3,a4,a5,a6,a7)
movdat <- data.frame(cbind(ag,mn))

b<-barplot(mn, names.arg = ag, 
           xlab = 'Age', 
           ylab='% Gets Most Military Knowledge From Pop Culture',
           ylim=c(0,0.2),
           col = c('gray40','gray40','gray40','gray40','gray40','gray40','gray40'))
text(b, mn+0.005, labels=as.character(round(mn, digits=2)))



#######################################################################



### Figure 8.2
# Wave 2
summary(factor(df$Q32))
summary(factor(df$AGE7))

df$MOVIES <- NA
df$MOVIES[df$Q32 < 77] <- 0
df$MOVIES[df$Q32 == 2] <- 1
summary(factor(df$MOVIES))

df$AGE2[df$AGE7 < 77] <- 1
df$AGE2[df$AGE7 < 3] <- 0

df$Q11T <- NA
df$Q11T[df$Q11 > 2 & df$Q11 < 77] <- 0
df$Q11T[df$Q11 < 3] <- 1
summary(factor(df$Q11T))

df2 <- df[!is.na(df$MOVIES),]
df2 <- df2[!is.na(df2$Q11T),]
summary(factor(df2$MOVIES))
summary(factor(df2$Q11T))

# Create Survey design
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )


# Calculate the mean confidence by age group and info type
yom <- svymean(~Q11T,design = w2_design[w2_design$variables$MOVIES==0 & w2_design$variables$AGE2==0])
yocl <- confint(svymean(~Q11T,design = w2_design[w2_design$variables$MOVIES==0 & w2_design$variables$AGE2==0]))[1]
yoch <- confint(svymean(~Q11T,design = w2_design[w2_design$variables$MOVIES==0 & w2_design$variables$AGE2==0]))[2]

ypm <- svymean(~Q11T,design = w2_design[w2_design$variables$MOVIES==1 & w2_design$variables$AGE2==0])
ypcl <- confint(svymean(~Q11T,design = w2_design[w2_design$variables$MOVIES==1 & w2_design$variables$AGE2==0]))[1]
ypch <- confint(svymean(~Q11T,design = w2_design[w2_design$variables$MOVIES==1 & w2_design$variables$AGE2==0]))[2]

oom <- svymean(~Q11T,design = w2_design[w2_design$variables$MOVIES==0 & w2_design$variables$AGE2==1])
oocl <- confint(svymean(~Q11T,design = w2_design[w2_design$variables$MOVIES==0 & w2_design$variables$AGE2==1]))[1]
ooch <- confint(svymean(~Q11T,design = w2_design[w2_design$variables$MOVIES==0 & w2_design$variables$AGE2==1]))[2]

opm <- svymean(~Q11T,design = w2_design[w2_design$variables$MOVIES==1 & w2_design$variables$AGE2==1])
opcl <- confint(svymean(~Q11T,design = w2_design[w2_design$variables$MOVIES==1 & w2_design$variables$AGE2==1]))[1]
opch <- confint(svymean(~Q11T,design = w2_design[w2_design$variables$MOVIES==1 & w2_design$variables$AGE2==1]))[2]

# Create plot
pt.est1 <- c(yom,ypm,oom,opm)
ci.low1 <- c(yocl,ypcl,oocl,opcl)
ci.high1 <- c(yoch,ypch,ooch,opch)
plot.labs <- c("Other Sources","Pop Culture","Other Sources ","Pop Culture ")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                       lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = 'black')+
  geom_hline(yintercept = 0, color = "black")+
  ylim(0.5,0.8)+
  ylab("Percent Confidence in the Military\n")+
  xlab("18-34 Years Old                                                                                 35 Years Old and Older")+
  theme_bw()
plot



#######################################################################



### Figure 8.3
df.mtf <- read_dta("MTF_Aggregate.dta")

# Line plot without points
ggplot(data=df.mtf, aes(x=var1))+
  geom_line(aes(y=confchange,linetype="Military Doing a Good Job"),
            size=1.03)+
  geom_line(aes(y=willchange,linetype="Will Probably Join the Military"),
            size=1.03)+
  geom_line(aes(y=defwillchange,linetype="Will Definitely Join the Military"),
            size=1.03)+
  scale_y_continuous(limits=c(-10,15))+
  scale_x_continuous(limits=c(1995,2019.5))+
  scale_linetype_manual(breaks=c("Military Doing a Good Job",
                                 "Will Probably Join the Military",
                                 "Will Definitely Join the Military"),
                        values = c(1,2,3))+
  geom_hline(yintercept=0,color='gray')+
  xlab("\nYear")+
  ylab("% Point Change From Previous Year")+
  theme(panel.grid = element_blank(), panel.background = element_blank(),
        panel.border = element_rect(fill=NA),
        text = element_text(size=16), legend.position = "bottom", 
        legend.title = element_blank())



#######################################################################



### Figure 8.4
df.mtf <- read_dta("MTF_Aggregate.dta")


# Line plot without points
gp1 <- ggplot(data=df.mtf, aes(x=var1))+
  geom_line(aes(y=good2,linetype="Good"),
            size=1.03)+
  geom_line(aes(y=fair2,linetype="Fair"),
            size=1.03)+
  geom_line(aes(y=poor2,linetype="Poor"),
            size=1.03)+
  scale_y_continuous(limits=c(0,25))+
  scale_x_continuous(limits=c(1995,2019.5))+
  scale_linetype_manual(breaks=c("Good","Fair","Poor"),values = c(1,2,3))+
  ggtitle("Definitely Will Join the Military")+
  xlab("\nYear")+
  ylab("\n% of Seniors Saying They Definitely Will Join\n")+
  theme(panel.grid = element_blank(), panel.background = element_blank(),
        panel.border = element_rect(fill=NA),
        text = element_text(size=14), legend.position = "bottom", 
        legend.title = element_blank(),
        plot.title = element_text(hjust = 0.5))

gp2 <- ggplot(data=df.mtf, aes(x=var1))+
  geom_line(aes(y=good,linetype="Good"),
            size=1.03)+
  geom_line(aes(y=fair,linetype="Fair"),
            size=1.03)+
  geom_line(aes(y=poor,linetype="Poor"),
            size=1.03)+
  scale_y_continuous(limits=c(0,25))+
  scale_x_continuous(limits=c(1995,2019.5))+
  scale_linetype_manual(breaks=c("Good","Fair","Poor"),values = c(1,2,3))+
  ggtitle("Probably Will Join the Military")+
  xlab("\nYear")+
  ylab("\n% of Seniors Saying They Probably Will Join\n")+
  theme(panel.grid = element_blank(), panel.background = element_blank(),
        panel.border = element_rect(fill=NA),
        text = element_text(size=14), legend.position = "bottom", 
        legend.title = element_blank(),
        plot.title = element_text(hjust = 0.5))

grid.arrange(gp1,gp2,ncol=2)



#######################################################################



### Figure 8.5
# Wave 2

summary(factor(df$Q11)) # Confidence
summary(factor(df$Q40)) # recommend mil serv
summary(factor(df$VETERAN))
summary(factor(df$Q37)) # worked/socialized in last month
summary(factor(df$Q38)) # worked/socialized in last year
summary(factor(df$Q39)) # family in military


svyby(~ Q40T, ~ Q11+VETERAN, w2_design_a, svymean, na.rm = TRUE)

df$Q11T <- NA
df$Q11T[df$Q11 > 2] <- 0
df$Q11T[df$Q11 < 3] <- 1
summary(factor(df$Q11T))

df$Q40T <- NA
df$Q40T[df$Q40 < 98] <- 0
df$Q40T[df$Q40 == 1] <- 1

df$Q37T <- NA
df$Q37T[df$Q37 < 3] <- 0
df$Q37T[df$Q37 == 1] <- 1

df$Q38T <- NA
df$Q38T[df$Q38 < 3] <- 0
df$Q38T[df$Q38 == 1] <- 1

df$Q39T <- NA
df$Q39T[df$Q39 < 3] <- 0
df$Q39T[df$Q39 == 1] <- 1

df2 <- df[!is.na(df$Q11T),]
df2 <- df2[!is.na(df2$Q40T),]
df2 <- df2[df2$P_ASSIGN1 == 1,]


df2a <- df2[df2$VETERAN<3,]
df2b <- df2[!is.na(df2$Q37T),]
df2c <- df2[!is.na(df2$Q38T),]
df2d <- df2[!is.na(df2$Q39T),]


# Create Survey design
w2_design_a <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2a
  )

w2_design_b <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2b
  )

w2_design_c <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2c
  )

w2_design_d <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2d
  )

# Create plot - vet civilian
v1 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==1 & w2_design_a$variables$VETERAN==1])
v2 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==2 & w2_design_a$variables$VETERAN==1])
v3 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==3 & w2_design_a$variables$VETERAN==1])
v4 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==4 & w2_design_a$variables$VETERAN==1])
gap <- NA
c1 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==1 & w2_design_a$variables$VETERAN==2])
c2 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==2 & w2_design_a$variables$VETERAN==2])
c3 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==3 & w2_design_a$variables$VETERAN==2])
c4 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==4 & w2_design_a$variables$VETERAN==2])
l1 <- "A great deal"
l2 <- "Quite a lot"
l3 <- "Some"
l4 <- "Very little"
l5 <- " "
l6 <- " A great deal"
l7 <- " Quite a lot"
l8 <- " Some"
l9 <- " Very little"

ag <- c(l1,l2,l3,l4,l5,l6,l7,l8,l9)
mna <- c(v1,v2,v3,v4,gap,c1,c2,c3,c4)
vetdat <- data.frame(cbind(ag,mna))


# Create plot - Mil in last month
m1 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==1 & w2_design_b$variables$Q37T==1])
m2 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==2 & w2_design_b$variables$Q37T==1])
m3 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==3 & w2_design_b$variables$Q37T==1])
m4 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==4 & w2_design_b$variables$Q37T==1])
gap <- NA
nm1 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==1 & w2_design_b$variables$Q37T==0])
nm2 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==2 & w2_design_b$variables$Q37T==0])
nm3 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==3 & w2_design_b$variables$Q37T==0])
nm4 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==4 & w2_design_b$variables$Q37T==0])

mnb <- c(m1,m2,m3,m4,gap,nm1,nm2,nm3,nm4)
monthdat <- data.frame(cbind(ag,mnb))


# Create plot - Mil in last year
y1 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==1 & w2_design_c$variables$Q38T==1])
y2 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==2 & w2_design_c$variables$Q38T==1])
y3 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==3 & w2_design_c$variables$Q38T==1])
y4 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==4 & w2_design_c$variables$Q38T==1])
gap <- NA
ny1 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==1 & w2_design_c$variables$Q38T==0])
ny2 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==2 & w2_design_c$variables$Q38T==0])
ny3 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==3 & w2_design_c$variables$Q38T==0])
ny4 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==4 & w2_design_c$variables$Q38T==0])

mnc <- c(y1,y2,y3,y4,gap,ny1,ny2,ny3,ny4)
yeardat <- data.frame(cbind(ag,mnc))



# Create plot - Family in mil
f1 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==1 & w2_design_d$variables$Q39T==1])
f2 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==2 & w2_design_d$variables$Q39T==1])
f3 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==3 & w2_design_d$variables$Q39T==1])
f4 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==4 & w2_design_d$variables$Q39T==1])
gap <- NA
nf1 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==1 & w2_design_d$variables$Q39T==0])
nf2 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==2 & w2_design_d$variables$Q39T==0])
nf3 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==3 & w2_design_d$variables$Q39T==0])
nf4 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==4 & w2_design_d$variables$Q39T==0])

mnd <- c(f1,f2,f3,f4,gap,nf1,nf2,nf3,nf4)
famdat <- data.frame(cbind(ag,mnd))



par(mfrow=c(2,2))
b1<-barplot(mna, names.arg = ag, 
            xlab = 'Veteran                                                                                                           Civilian', 
            ylab='% Who Would Advise a Family Member to Join',
            ylim=c(0,1.01),
            col = c('gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40'))
text(b1, mna+0.025, labels=as.character(round(mna, digits=2)))

b2<-barplot(mnb, names.arg = ag, 
            xlab = 'Worked With Mil in Last Month                                                                        No Contact', 
            ylab='% Who Would Advise a Family Member to Join',
            ylim=c(0,1.01),
            col = c('gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40'))
text(b2, mnb+0.025, labels=as.character(round(mnb, digits=2)))

b3<-barplot(mnc, names.arg = ag, 
            xlab = 'Worked With Mil in Last Year                                                                                 No Contact', 
            ylab='% Who Would Advise a Family Member to Join',
            ylim=c(0,1.01),
            col = c('gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40'))
text(b3, mnc+0.025, labels=as.character(round(mnc, digits=2)))

b4<-barplot(mnd, names.arg = ag, 
            xlab = 'Family in Military                                                                                                     No Family in Military', 
            ylab='% Who Would Advise a Family Member to Join',
            ylim=c(0,1.01),
            col = c('gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40'))
text(b4, mnd+0.025, labels=as.character(round(mnd, digits=2)))



#######################################################################



### Figure 8.6
df.ds <- read_dta("Defense_Spending.dta")

# Line plot without points
ggplot(data=df.ds, aes(x=year))+
  geom_line(aes(y=defensespendingistoolow, linetype="DEFENSE SPENDING IS TOO LOW"),
            size=1.0)+
  geom_line(aes(y=greatdealofconfidenceinmilitary, linetype="GREAT DEAL OF CONFIDENCE IN MILITARY"),
            size=1.0)+
  scale_y_continuous(limits=c(0,0.51))+
  scale_x_continuous(limits=c(1969,2021))+
  scale_linetype_manual(breaks=c("DEFENSE SPENDING IS TOO LOW",
                                 "GREAT DEAL OF CONFIDENCE IN MILITARY"),
                        values = c(1,2))+
  xlab("\nYear")+
  ylab("Percentage of Respondents\n")+
  theme(panel.grid = element_blank(), panel.background = element_blank(),
        panel.border = element_rect(fill=NA),
        text = element_text(size=16), legend.position = "bottom", 
        legend.title = element_blank())



#######################################################################



### Figure 8.7
df.anes <- read_sav("anes_timeseries_cdf.sav")

df.anes$DEM <- 0
df.anes$DEM[df.anes$VCF0303 == 1] <- 1

df.anes$REP <- 0
df.anes$REP[df.anes$VCF0303 == 3] <- 1

df.anes$IND <- 0
df.anes$IND[df.anes$VCF0303 == 2] <- 1

df.anes$post911 <- 0
df.anes$post911[df.anes$VCF0004 > 2000] <- 1

df.anes.dem <- df.anes[df.anes$VCF0843 < 8 & df.anes$DEM == 1 & !is.na(df.anes$VCF0843),]
df.anes.rep <- df.anes[df.anes$VCF0843 < 8 & df.anes$REP == 1 & !is.na(df.anes$VCF0843),]

# Line plot without points
ggdem <- ggplot(data=df.anes.dem, aes(y=VCF0843,x=VCF0213))+
  geom_smooth(method='lm',mapping = aes(group=post911,weight = VCF0009z))+
  scale_y_continuous(limits=c(0,7))+
  scale_x_continuous(limits=c(0,100))+
  xlab("\nThermometer - Military")+
  ylab("Support for Defense Spending\n")+
  ggtitle("Democrats")+
  theme(panel.grid = element_blank(), panel.background = element_blank(),
        panel.border = element_rect(fill=NA),
        text = element_text(size=16), legend.position = "bottom", 
        legend.title = element_blank())


ggrep <- ggplot(data=df.anes.rep, aes(y=VCF0843,x=VCF0213))+
  geom_smooth(method='lm',mapping = aes(group=post911,weight = VCF0009z))+
  scale_y_continuous(limits=c(0,7))+
  scale_x_continuous(limits=c(0,100))+
  xlab("\nThermometer - Military")+
  ylab("")+
  ggtitle("Republicans")+
  theme(panel.grid = element_blank(), panel.background = element_blank(),
        panel.border = element_rect(fill=NA),
        text = element_text(size=16), legend.position = "bottom", 
        legend.title = element_blank())

grid.arrange(ggdem,ggrep,ncol=2)



#######################################################################



### Figure 8.8
summary(factor(df$Q11T))
summary(factor(df$Q4D))
summary(factor(df$Q4AD))

df$Q4D2 <- NA
df$Q4D2[df$Q4D < 77] <- 0.5
df$Q4D2[df$Q4D < 3] <- 1
df$Q4D2[df$Q4D > 3 & df$Q4D < 77] <- 0

df$Q4AD2 <- NA
df$Q4AD2[df$Q4AD < 77] <- 0.5
df$Q4AD2[df$Q4AD < 3] <- 1
df$Q4AD2[df$Q4AD > 3 & df$Q4AD < 77] <- 0

df_Q4D2 <- df[!is.na(df$Q4D2) & df$Q11 < 77 & !is.na(df$party),]
df_Q4AD2 <- df[!is.na(df$Q4AD2) & df$Q11 < 77 & !is.na(df$party),]

# Create Survey design
w2_design_a <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df_Q4D2
  )

w2_design_ad <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df_Q4AD2
  )

fig88_Q4D2_means <- svyby(~ Q4D2, ~ Q11T+party, w2_design_a, svymean, na.rm = TRUE)
fig88_Q4AD2_means <- svyby(~ Q4AD2, ~ Q11T+party, w2_design_ad, svymean, na.rm = TRUE)


g.D2 <- ggplot(fig88_Q4D2_means,aes(x=factor(party),y=Q4D2,fill=factor(Q11T),label=round(Q4D2,digits=2)))+
  geom_col(position = position_dodge())+
  geom_text(size=3,position=position_dodge(1),vjust=-.5)+
  ylim(0,1)+
  theme(legend.position = "null")+
  scale_x_discrete(labels=c("Democrat", 'Independent', "Republican"))+
  xlab("")+
  ylab("Mean Support for Cutting State to Fund DoD")

g.AD2 <- ggplot(fig88_Q4AD2_means,aes(x=factor(party),y=Q4AD2,fill=factor(Q11T),label=round(Q4AD2,digits=2)))+
  geom_col(position = position_dodge())+
  geom_text(size=3,position=position_dodge(1),vjust=-.5)+
  ylim(0,1)+
  scale_fill_discrete(name="Confidence\nin Military",
                      breaks=c(0,1),
                      labels=c("Not Confident","Confident"))+
  scale_x_discrete(labels=c("Democrat", 'Independent', "Republican"))+
  xlab("")+
  ylab("Mean Support for Cutting DoD to Fund State")

grid.arrange(g.D2,g.AD2,ncol=2,widths=c(1,1.2))



#######################################################################



### Figure 8.9
summary(factor(df$Q11))
summary(factor(df$Q2))
summary(factor(df$Q2A))

df2 <- df[df$Q11 < 77 & !is.na(df$Q2),]
df2 <- df2[df2$Q2 < 77,]

df2a <- df[df$Q11 < 77 & !is.na(df$Q2A),]
df2a <- df2a[df2a$Q2A < 77,]

# Create Survey design
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )

w2_design_1 <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2a
  )

summary(factor(w2_design$variables$Q2))

Q2_tot <- svytable(~Q2,w2_design)
dod_perc <- data.frame(budget=factor(c("$20 Billion","$55 Billion","$250 Billion",
                                       "$450 Billion","$700 Billion",'$1.1 Trillion'), 
                                     levels=c("$20 Billion","$55 Billion","$250 Billion",
                                              "$450 Billion","$700 Billion",'$1.1 Trillion')),
                       percent=round(Q2_tot/sum(Q2_tot)*100))

Q2A_tot <- svytable(~Q2A,w2_design_1)
dos_perc <- data.frame(budget=factor(c("$20 Billion","$55 Billion","$250 Billion",
                                       "$450 Billion","$700 Billion",'$1.1 Trillion'), 
                                     levels=c("$20 Billion","$55 Billion","$250 Billion",
                                              "$450 Billion","$700 Billion",'$1.1 Trillion')),
                       percent=round(Q2A_tot/sum(Q2A_tot)*100))



g.dod <- ggplot(dod_perc,aes(x="",y=percent.Freq,fill=budget))+
  geom_bar(stat="identity",width=1,color='white')+
  coord_polar("y",start=0)+
  scale_fill_brewer(palette='Blues')+
  theme_void()+
  geom_text(aes(y=c(95,85,71,53,34,12),label=percent.Freq),color='black',size=4.5)+
  ggtitle("Department of Defense")+
  theme(legend.position = "null",plot.title = element_text(hjust=0.5))

g.dos <- ggplot(dos_perc,aes(x="",y=percent.Freq,fill=budget))+
  geom_bar(stat="identity",width=1,color='white')+
  coord_polar("y",start=0)+
  scale_fill_brewer(palette='Blues')+
  theme_void()+
  geom_text(aes(y=c(92,79,63,45,31,13),label=percent.Freq),color='black',size=4.5)+
  ggtitle("Department of State")+
  theme(plot.title = element_text(hjust=0.5))

grid.arrange(g.dod,g.dos,ncol=2,widths=c(1,1.2))



#######################################################################



### Figure 8.10
summary(factor(df$Q11))
summary(factor(df$Q2))

# A great deal
df.gd <- df[df$Q11 == 1 & !is.na(df$Q2),]
df.gd <- df.gd[df.gd$Q2 < 77,]

# Quite a lot
df.qa <- df[df$Q11 == 2 & !is.na(df$Q2),]
df.qa <- df.qa[df.qa$Q2 < 77,]

# Some
df.s <- df[df$Q11 == 3 & !is.na(df$Q2),]
df.s <- df.s[df.s$Q2 < 77,]

# Very little
df.vl <- df[df$Q11 == 4 & !is.na(df$Q2),]
df.vl <- df.vl[df.vl$Q2 < 77,]

# Create Survey design
# A great deal
w2_design.gd <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df.gd
  )

# Quite a lot
w2_design.qa <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df.qa
  )

# Some
w2_design.s <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df.s
  )

# Very little
w2_design.vl <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df.vl
  )

# A great deal
Q2_gd <- svytable(~Q2,w2_design.gd)
df.gd <- data.frame(budget=factor(c("$20 Billion","$55 Billion","$250 Billion",
                                    "$450 Billion","$700 Billion",'$1.1 Trillion'), 
                                  levels=c("$20 Billion","$55 Billion","$250 Billion",
                                           "$450 Billion","$700 Billion",'$1.1 Trillion')),
                    percent=round(Q2_gd/sum(Q2_gd)*100))

# Quite a lot
Q2_qa <- svytable(~Q2,w2_design.qa)
df.qa <- data.frame(budget=factor(c("$20 Billion","$55 Billion","$250 Billion",
                                    "$450 Billion","$700 Billion",'$1.1 Trillion'), 
                                  levels=c("$20 Billion","$55 Billion","$250 Billion",
                                           "$450 Billion","$700 Billion",'$1.1 Trillion')),
                    percent=round(Q2_qa/sum(Q2_qa)*100))


# Some
Q2_s <- svytable(~Q2,w2_design.s)
df.s <- data.frame(budget=factor(c("$20 Billion","$55 Billion","$250 Billion",
                                   "$450 Billion","$700 Billion",'$1.1 Trillion'), 
                                 levels=c("$20 Billion","$55 Billion","$250 Billion",
                                          "$450 Billion","$700 Billion",'$1.1 Trillion')),
                   percent=round(Q2_s/sum(Q2_s)*100))


# Very little
Q2_vl <- svytable(~Q2,w2_design.vl)
df.vl <- data.frame(budget=factor(c("$20 Billion","$55 Billion","$250 Billion",
                                    "$450 Billion","$700 Billion",'$1.1 Trillion'), 
                                  levels=c("$20 Billion","$55 Billion","$250 Billion",
                                           "$450 Billion","$700 Billion",'$1.1 Trillion')),
                    percent=round(Q2_vl/sum(Q2_vl)*100))


# Pie Charts

g.gd <- ggplot(df.gd,aes(x="",y=percent.Freq,fill=budget))+
  geom_bar(stat="identity",width=1,color='white')+
  coord_polar("y",start=0)+
  scale_fill_brewer(palette='Blues')+
  theme_void()+
  geom_text(aes(y=c(95.5,87,72,53,33.5,11),label=percent.Freq),color='black',size=4.5)+
  ggtitle("A great deal")+
  theme(legend.position = "null",plot.title = element_text(hjust=0.5))

g.qa <- ggplot(df.qa,aes(x="",y=percent.Freq,fill=budget))+
  geom_bar(stat="identity",width=1,color='white')+
  coord_polar("y",start=0)+
  scale_fill_brewer(palette='Blues')+
  theme_void()+
  geom_text(aes(y=c(95,85,71,52,30,9),label=percent.Freq),color='black',size=4.5)+
  ggtitle("Quite a lot")+
  theme(legend.position = "null",plot.title = element_text(hjust=0.5))

g.s <- ggplot(df.s,aes(x="",y=percent.Freq,fill=budget))+
  geom_bar(stat="identity",width=1,color='white')+
  coord_polar("y",start=0)+
  scale_fill_brewer(palette='Blues')+
  theme_void()+
  geom_text(aes(y=c(93.5,82,68,51.5,34.5,12.5),label=percent.Freq),color='black',size=4.5)+
  ggtitle("Some")+
  theme(legend.position = "null",plot.title = element_text(hjust=0.5))

g.vl <- ggplot(df.vl,aes(x="",y=percent.Freq,fill=budget))+
  geom_bar(stat="identity",width=1,color='white')+
  coord_polar("y",start=0)+
  scale_fill_brewer(palette='Blues')+
  theme_void()+
  geom_text(aes(y=c(93,84,74.5,63,48,18.5),label=percent.Freq),color='black',size=4.5)+
  ggtitle("Very little")+
  theme(legend.position = c(-.12,0.2),plot.title = element_text(hjust=0.5))

grid.arrange(g.gd,g.qa,g.s,g.vl,ncol=2,nrow=2,widths=c(1,1))
